package com.xumt.hwHMSdemo;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import androidx.appcompat.app.AppCompatActivity;

import com.xumt.hwHMSdemo.cannectSb.HttpRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Shoppingcard extends AppCompatActivity {
    private String result;
   private SimpleAdapter adapter = null;
  private   List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shoppingcard);

        adapter = new MySimpleAdapter(this, getData(), R.layout.mylist1, new String[]{"objectname", "objectprice", "objectnumber","objectimage"},
                new int[]{R.id.objectname, R.id.objectprice, R.id.objectnumber,R.id.objectimage});
        ListView listView = (ListView) findViewById(R.id.shoppingcard);
        listView.setAdapter(adapter);
    }


    private class MySimpleAdapter extends SimpleAdapter {
        public MySimpleAdapter(Context context,
                               List<? extends Map<String, ?>> data, int resource,
                               String[] from, int[] to) {
            super(context, data, resource, from, to);
            // TODO Auto-generated constructor stub
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            final int mPosition = position;
            convertView = super.getView(position, convertView, parent);

            Button buttonadd=(Button)convertView.findViewById(R.id.add);
            buttonadd.setOnClickListener(new View.OnClickListener() {

                                             @Override
                                             public void onClick(View v) {
                                                 Map<String, Object>map=list.get(mPosition);
                                                 int i=(int)map.get("objectnumber");
                                                if(i<999) map.put("objectnumber",i+1 );//将新的文件名添加到Map以替换旧文件名
                                                 list.set(mPosition, map);//替换listItems中原来的map
                                                 adapter.notifyDataSetChanged();//通知SimpleAdapter数据改变
                                             }
                                         });

            Button buttonredure=(Button)convertView.findViewById(R.id.redure);
            buttonredure.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Map<String, Object>map=list.get(mPosition);
                    int i=(int)map.get("objectnumber");
                    if(i>1)   map.put("objectnumber",i-1 );//将新的文件名添加到Map以替换旧文件名
                    list.set(mPosition, map);//替换listItems中原来的map
                    adapter.notifyDataSetChanged();//通知SimpleAdapter数据改变
                }
            });
            return convertView;
        }
    }

        private List<Map<String, Object>> getData() {
            new Thread() {
                public void run() {
                    try {
                        HttpRequest request = new HttpRequest();
                        result = request.sendGet("http://169.254.67.221:8081/Object/userobject",
                                "username="+Login2.usernamen);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }.start();

            try {
                Thread.sleep(500);
                JSONArray jsonArray = new JSONArray(result);
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("objectname", "商品：" + jsonObject.getString("objectname"));
                    map.put("objectprice", "单价：" + jsonObject.getString("objectprice"));
                    map.put("objectnumber", 1);
                    Context ctx=getBaseContext();
                    int resId = getResources().getIdentifier(jsonObject.getString("image"), "drawable" ,ctx.getPackageName());
                    map.put("objectimage",resId);
                    list.add(map);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return list;
        }

}